package com.dingdang.service.impl;

import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.TbCarGoods;
import com.dingdang.mapper.*;
import com.dingdang.service.FreemarkerService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FreemarkerServiceImpl implements FreemarkerService {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private TbCarGoodsTwoMapper tbCarGoodsTwoMapper;

    @Override
    @Async("tarkExecutor")
    public void createPage(Long carId) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fw = null;
        try {
            //Template template = configuration.getTemplate("item.ftl");
            System.out.println("生成静态页面");
            Map map = new HashMap();
            TbCarGoods goods = tbCarGoodsTwoMapper.selectInfoById(carId);
            System.out.println(goods);
            map.put("goods",goods);
            fw = new FileWriter("D:\\goods\\" + carId + ".html");
          //  template.process(map, fw);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
